class MembersController < ApplicationController
  before_filter :find_project
  
  def index
    @users = User.find(:all)
    if @project
      @member_ids = @project.members.collect { |user| user.id}
    end
      
  end
  
  def update
    ids = params[:ids]
    
    ActiveRecord::Base.connection.execute("DELETE FROM members_projects WHERE project_id = #{@project.id}")
    
    if ids
      ids.each do |user_id|
        ActiveRecord::Base.connection.execute("INSERT INTO members_projects(project_id,user_id,created_at,updated_at) VALUES(#{@project.id},#{user_id},NOW(),NOW())")
      end
    end
    
    flash[:notice] = "Team members updated."
    redirect_to project_members_url(@project)
  
  end
  
  private
  def find_project
    @project = Project.find_by_id(params[:project_id])
  end
  
end